var moment = require('moment');
moment.locale('zh-cn');
var connection = require('./baseDB');

module.exports = {
  // addNewGame(obj, callback) { // 新增游戏
  //   // console.log(obj);
  //   // var obj=obj
  //   var sqlStr = 'insert into gamelist set ?';
  //   connection(sqlStr, obj, (err, results) => {
  //     if (err) return callback(err);
  //     console.log(results);
  //     return callback(null, results);
  //   });
  // },
  // getAllgame(nowPage, pageSize, callback) { // 先获取所有的游戏，然后在改造成 获取分页
  //   // var sqlStr = 'select articles.*, users.nickname from articles left join users on articles.authorId=users.id order by ctime desc';
  //   // 偏移量的算法：  (当前页码值 - 1) * 每页显示几条数据
  //   console.log(nowPage + "~~~" + pageSize);
  //   var offset = (nowPage - 1) * pageSize;
  //   var sqlStr = 'select * ,(select count(*)  from gamelist) AS totalCount from gamelist  where isDelete=0 order by c_time LIMIT ' + offset + ',' + pageSize;
  //   connection(sqlStr, (err, results) => {
  //     console.log(err);
  //     if (err) return err;

  //     // 循环文章列表处理时间
  //     // results[0].forEach(item => {
  //     //   item.ctime = moment(item.ctime).fromNow();
  //     // });
  //     callback(null, results);
  //   });
  // // },
  // getGameType(type, callback) { // 根据类型选择游戏ß
  //   var sqlStr = 'select * from gamelist where  isDelete=0 and  game_type= ?';
  //   connection(sqlStr, type, (err, results) => {
  //     if (err) return callback(err);
  //     // 循环文章列表处理时间ßßßß
  //     // results[0].forEach(item => {
  //     //   item.ctime = moment(item.ctime).fromNow();
  //     // });
  //     // console.log(results);

  //     callback(null, results);
  //   });
  // },
  /**
   *    获取最新游戏
   */
  // getNewGame(callback) {

  //   var sqlStr = 'select * from gamelist   isDelete=0   order by c_time desc ';
  //   connection(sqlStr, (err, results) => {
  //     if (err) return callback(err);

  //     callback(null, results);
  //   });
  // },
  /**
   *  根据对象查询
   * @param {*} obj 
   * @param {*} callback 
   */
  sort(obj, callback) {
    var sqlStr = 'select * from  Main  where ?';
    connection(sqlStr, obj, (err, results) => {
      if (err) return callback(err);

      callback(null, results);
    });
  },
  /**
   *     字母模糊匹配
   * @param {*} str 
   * @param {*} callback 
   */
  fuzzyQuery(strLike, callback) {
    console.log(strLike + "~~~mode");

    var sqlStr = ' select * from gamelist where isDelete=0 and en_name like ?';
    // var sqlStr= 'select * from gamelist where ?';
    connection(sqlStr, "%" + strLike + "%", (err, results) => {
      if (err) return callback(err);
      console.log(results);

      callback(null, results);
    });
  },

  ISOut(str, callback) {
    var sqlStr = str == '1' ?
        'SELECT * from  Main where IsInterviewed=0 and ISOutource=1 ORDER BY date ;'
      : 'SELECT * from  Main where IsInterviewed=0 and ISOutource=0 ORDER BY date ;'
    connection(sqlStr, (err, results) => {
      if (err) return callback(err);
      callback(null, results);
    })
  }
}
 